<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="操作地址" prop="ipaddr">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入操作地址"
            clearable
            style="width: 230px;"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="users">
          <el-input
            v-model="queryParams.users"
            placeholder="请输入用户名称"
            clearable
            style="width: 230px;"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button  @click="resetQuery">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
          <el-button type="success" @click="exportExcel">
            <el-icon><Download /></el-icon>导出 Excel 表格
          </el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8"  style="margin-bottom: 20px;">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            size="mini"
            @click="showdialog"
          ><el-icon><Delete /></el-icon>删除
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="opsdata.slice(startIndex, endIndex)" border stripe style="width: 100%" v-loading="loading" :header-cell-style="{backgroundColor:'aliceblue',color:'#666'}">
      <el-table-column label="操作时间" prop="time" align="center" sortable></el-table-column>
      <el-table-column label="操作地址" prop="ip" align="center"></el-table-column>
      <el-table-column label="操作信息" prop="operation" align="center"></el-table-column>
      <el-table-column label="操作类型" prop="type" align="center"></el-table-column>
      <el-table-column label="用户名称" prop="users" align="center"></el-table-column>
      <el-table-column label="其他" prop="qita" align="center"></el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      :page-size="pageSize"
      :total="opsdata.length"
      @current-change="handlePageChange"
      style="margin-top: 20px;"
    ></el-pagination>
    </div>
    <!-- 删除警告弹窗 -->
    <el-dialog v-model="show" title="删除确认">
        <main>确定要删除该条数据吗？</main>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="show = false">取消</el-button>
                    <el-button type="primary" @click="handleDelete">确认</el-button>
                </span>
            </template>
        </el-dialog>
  </template>
  
  <script>
  import { list,logSearch,logSearchIp,Deleteall} from "@/api/monitor/loginlog";
  import * as XLSX from 'xlsx';

  export default {
  name: "LoginLog",
  dicts: ['sys_common_status'],
  data() {
    return {
      dict: {
        type: {}
      },
      show:false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      opsdata: [], // 数据源
      startIndex: 0,
      endIndex: 10, // 每页显示数量
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示数量
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        ipaddr: undefined,
        users: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开删除警告弹窗
    showdialog(){
      this.show=true;
    },
    // 清空日志
    handleDelete(){
      Deleteall().then(res=>{
        this.getList();
        if(res.code==200){
          this.$message({
          message: '清空成功',
        });
        this.show=false;
        }
      })
    },
    handlePageChange(newPage) {
      this.currentPage = newPage;
      this.startIndex = (newPage - 1) * this.pageSize;
      this.endIndex = this.startIndex + this.pageSize;
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list().then(response => {
          console.log(response)
          // this.list = response.data.data;
          this.opsdata=response.data.data;
          console.log(this.list)
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.loading = true;
      console.log(this.queryParams);
      if  (this.queryParams.ipaddr) {
        logSearchIp({address:this.queryParams.ipaddr}).then(response => {
        console.log(response.data.data);
        this.opsdata = response.data.data;
        console.log(this.list);
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error('查询失败：', error);
        this.loading = false;
        this.list = []; // 清空数据
        this.total = 0;
        this.$message({
          message: '查询失败',
          type: 'warning',
        });
      });
      }
      if (this.queryParams.users){
      logSearch({users:this.queryParams.users}).then(response => {
        console.log(response.data.data);
        this.opsdata = response.data.data;
        console.log(this.list);
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error('查询失败：', error);
        this.loading = false;
        this.list = []; // 清空数据
        this.total = 0;
        this.$message({
          message: '查询失败',
          type: 'warning',
        });
      });
    }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.getList();
      this.$resetForm("queryForm");
      this.queryParams.pageNum = 1;
    },
    exportExcel() {
      const excelData = this.opsdata.map((item) => ({
        '操作时间': item.time,
        '操作地址': item.ip,
        '操作信息': item.operation,
        '操作类型': item.type,
        '用户名称': item.users,
        '其他': item.qita,
      }));
      // 创建工作簿
      const ws = XLSX.utils.json_to_sheet(excelData);
      // 创建工作簿名称
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, ws, '操作日志');
      // 导出 Excel 文件
      XLSX.writeFile(wb, '操作日志.xlsx');
    },
  }
};
  </script>
  
  <style scoped>
  .el-form{
    display: flex;
    flex-wrap:wrap;
  }
  </style>